package cn.biplam.front.component.view.systemManage.modelset.manage;

import cn.biplam.common.bean.md.ModelThemeRootObj;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.view.common.DbSourceSelect;
import cn.biplam.front.component.view.systemManage.modelset.ModelSetManage;
import cn.biplam.front.html.HtmlText;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Length_Unit;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.ScriptCustomer;
import cn.biplam.front.htmlscript.script.ScriptPrompt.Close_Prompt;
import cn.biplam.front.template.base.SimpleInput;
import cn.biplam.front.template.base.SimpleTextArea;
import cn.biplam.front.template.control.FlatTable;

public class ModelThemeAdd extends AjxResponse{
	
	/** 组件ID */
	public static String cId="8206";
	/** 获取添加模型主题界面动作ID */
	public static String Add_Theme="820601";
	/** 添加添加模型主题操作动作ID */
	public static String Add_Theme_Opt="820602";
	private String width = "420";
	
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(actionId.equals(Add_Theme)){
			return _getAddTheme();
		}else if(actionId.equals(Add_Theme_Opt)){
			return _addOpt();
		}else{
			throw new ErrorMessage("Ajx Exchange outport not found:"+ actionId+" !");
		}
	}
	
	/**
	 * 获取添加数据源界面
	 * @return	HtmlWindow
	 * @throws ErrorMessage
	 */
	private HtmlWindow _getAddTheme() throws ErrorMessage{
		String modelType = this.recordId;
		ModelThemeRootObj mtro = ModelThemeRootObj.getNewInstance(mUser,modelType);
		HtmlWindow panel=_drawPanel(mUser,null,sHomeUrl,mtro);
//		ScriptCustomer.getSpSubmitCheckInput(getFieldCheck(mtro),panel,cId, Add_Theme_Opt,containerId,Is_RefreshParent.NoRefresh,
//				Is_AddParam.NoAdd,Close_Prompt.Close);
		ScriptCustomer.getSpSubmitCheckInput(getFieldCheck(mtro),panel,cId, Add_Theme_Opt,Close_Prompt.Close);
		panel.setStyleWidth(width);
		/** 设置隐藏表单*/
		panel.put(new HtmlValue(null,"h,modelType",modelType));
		panel.put(new HtmlValue(null,"h,"+mtro.getTHEME_TYPE(),params.getByKey(mtro.getTHEME_TYPE())));
		return panel;
	}
	
	/**
	 * 数据源信息添加或编辑界面 
	 * @throws ErrorMessage
	 */
	private HtmlWindow _drawPanel(UserInfo mUser,OperatorResult sr,String sHomeUrl,ModelThemeRootObj mtro) throws ErrorMessage{
		String themeName=null;
		String themeDesc = null;
		String dsid=null;
	
		if(TOOLS.exist(sr)){
			themeName = sr.getValue(mtro.getTHEME_NAME());
			themeDesc = sr.getValue(mtro.getTHEME_DESC());
			dsid = sr.getValue(mtro.getDSID());			
		}
		
		FlatTable table = new FlatTable(cId,recordId);
		
		table.putTh("主题名称",0,0);
		table.putTd(new SimpleInput(mtro.getTHEME_NAME(),"52",themeName).get(), 0, 1);
		table.putTh("主题说明",1,0);
		table.putTd(new SimpleTextArea(mtro.getTHEME_DESC(),"50",themeDesc).get(),1,1);
		table.putTh("数据源",2,0);
		table.putTd(DbSourceSelect.get(mtro.getDSID(), dsid, mUser, sHomeUrl), 2, 1);
		
		table.setColWidth(0,"20",Length_Unit.percent);
		table.setWidth(width);

		return table.get();
	}
	
	public HtmlWindow _addOpt() throws ErrorMessage{
		/** 模型类型*/
		String modelType = params.getByKey("modelType");
		HtmlWindow win=new HtmlText();
		ModelThemeRootObj mtro = ModelThemeRootObj.getNewInstance(mUser,modelType);		
		DataFilter filter=new DataFilter();
		
		filter.put(mtro.getDSID(), params.getByKey(mtro.getDSID()+"value"));
		filter.putAll(this.params);	
		/** 插入新主题信息*/
		mtro.insert(filter);
		
		win.bind(AjxScript.getA(ModelSetManage.cId,ModelSetManage.aId_main,modelType));
		
		return win;
	}
	
	/**
	 * 取需要检查的表单
	 * @return
	 * @throws ErrorMessage
	 */
	private KvMap<Integer,String[]> getFieldCheck(ModelThemeRootObj mtroi) throws ErrorMessage{
		KvMap<Integer,String[]> fields = new KvMap<Integer,String[]>();
		fields.put(0,new String[]{mtroi.getTHEME_NAME(), "请输入主题名称"});
		fields.put(1,new String[]{mtroi.getTHEME_DESC(), "请输入主题描述"});
		fields.put(2,new String[]{"h,"+mtroi.getDSID()+"value", "请选择数据源"});
		return fields;
	}
}
